#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: dag01.py 
@version:
@time: 2022/01/06 
"""
from TapClientCore.T03_api_body_templates.v1_api_body import DATAFLOW_NODE_CLONE_SOURCE, DATAFLOW_NODE_CLONE_SINK, \
    DATAFLOW_NODE_SYNC_SOURCE, DATAFLOW_NODE_SYNC_SINK, DATAFLOW_NODE_SYNC_JOINTABLE, DATAFLOW_NODE_SYNC_SINK_REDIS
from TapClientCore.T04_logic.Dag01.node.common_clone_sink_node import CommonCloneSinkNode
from TapClientCore.T04_logic.Dag01.node.common_clone_source_node import CommonCloneSourceNode
from TapClientCore.T04_logic.Dag01.node.common_sync_sink_node import CommonSyncSinkNode
from TapClientCore.T04_logic.Dag01.node.common_sync_source_node import CommonSyncSourceNode
from TapClientCore.T04_logic.Dag01.node.sync_javascript_node import SyncSinkJavaScriptNode
from TapClientCore.T04_logic.Dag01.node.sync_sink_redis_node import SyncSinkRedisNode
from TapClientCore.T04_logic.Dag01.relations.relation import MapTable, Association


def generate_stage(obj):
    obj_type = type(obj)
    # clone source
    if obj_type == CommonCloneSourceNode:
        tmp = DATAFLOW_NODE_CLONE_SOURCE.copy()
        tmp.update(obj.attribute)
        return tmp
    # clone sink
    elif obj_type == CommonCloneSinkNode:
        tmp = DATAFLOW_NODE_CLONE_SINK.copy()
        tmp.update(obj.attribute)
        return tmp
    # sync source
    elif obj_type == CommonSyncSourceNode:
        tmp = DATAFLOW_NODE_SYNC_SOURCE.copy()
        tmp.update(obj.attribute)
        return tmp
    # sync sink
    elif obj_type == CommonSyncSinkNode:
        tmp = DATAFLOW_NODE_SYNC_SINK.copy()
        tmp.update(obj.attribute)
        return tmp
    # redis sync sink
    elif obj_type == SyncSinkRedisNode:
        return obj.attribute
    elif obj_type == SyncSinkJavaScriptNode:
        return obj.attribute


class DAG01:
    def __init__(self):
        self.stages = []
        self.lastSource = None

    def edge(self, source, sink, relations):
        if not self.lastSource:
            self.lastSource = source

        _source = None
        _sink = None

        for stage_items in self.stages:
            if stage_items["id"] == source.id:
                _source = stage_items
            if stage_items["id"] == sink.id:
                _sink = stage_items

        if _source is None:
            _source = generate_stage(source)
            self.stages.append(_source)

        if _sink is None:
            _sink = generate_stage(sink)
            self.stages.append(_sink)

        _source["outputLanes"].append(sink.id)
        _sink["inputLanes"].append(source.id)
        # clone relations
        if type(relations) == MapTable:
            _sink["syncObjects"] = [{
                "type": "table",
                "objectNames": self.lastSource.table,
            }]
            _sink["table_prefix"] = relations.prefix
            _sink["table_suffix"] = relations.suffix
            _sink["dropType"] = relations.dropType
            _sink['tableNameTransform'] = relations.tableNameTransform
            _sink['fieldsNameTransform'] = relations.fieldsNameTransform

        if type(relations) == Association:
            if relations.associationType == 'redis':
                _sink['redisKeyPrefix'] = relations.association
            else:
                joinTables = {
                    "joinType": relations.joinType,
                    "joinPath": relations.joinPath,
                    "manyOneUpsert": relations.manyOneUpsert,
                    "joinKeys": [{"source": m[0], "target": m[1]} for m in relations.joinKeys],
                    "arrayUniqueKey": relations.arrayUniqueKey,
                    "isArray": relations.isArray,
                    "connectionId": self.lastSource.attribute["connectionId"],
                    "stageId": self.lastSource.attribute["id"],
                    "tableName": self.lastSource.table,
                    "tableId": self.lastSource.tableId,

                }
                tmp = DATAFLOW_NODE_SYNC_JOINTABLE.copy()
                for attr in joinTables:
                    tmp[attr] = joinTables[attr]
                _sink["joinTables"].append(tmp)


if __name__ == '__main__':
    import pprint

    S = CommonCloneSourceNode("auto_mysql", "my2")
    T = CommonCloneSinkNode("auto_mysql")
    d = DAG01()
    d.edge(S, T, MapTable())
    pprint.pprint(d.stages)
